package com.example.demo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.PositionEntity;
import com.example.demo.mapper.PositionMapper;
import com.example.demo.util.CIOUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PositionService {

    @Autowired
    PositionMapper positionMapper;

    public List<PositionEntity> queryListAll() {
        return positionMapper.queryListAll();
    }

    public List<PositionEntity> queryByCondition(PositionEntity positionEntity) {
        QueryWrapper<PositionEntity> queryWrapper = new QueryWrapper<PositionEntity>();
        queryWrapper.eq("flg_del", 0);
        queryConditionSet(positionEntity, queryWrapper);
        List<PositionEntity> positionList = positionMapper.selectList(queryWrapper);
        return positionList;

    }


    public void queryConditionSet(PositionEntity positionEntity, QueryWrapper<?> queryWrapper) {
        if (positionEntity != null) {

            String positionType = positionEntity.getPositionType();
            if (positionType != null && !positionType.trim().equals("")) {

                queryWrapper.like("position_type", positionType);
            }

            String workType = positionEntity.getWorkType();
            if (workType != null && !workType.trim().equals("")) {

                queryWrapper.like("work_type", workType);
            }
        }
    }


    public String updatePosition(PositionEntity positionEntity) {
        if (positionEntity != null && positionEntity.getId() != null
                && !positionEntity.getId().equals("")) {

            positionMapper.updateById(positionEntity);
        }
        return CIOUtil.buildRetMsg(true, "更新成功", null);
    }

    public String deletePosition(String[] ids) {
        if (ids != null) {

            for (String id : ids) {
                PositionEntity positionEntity = new PositionEntity();
                positionEntity.setId(id);
                positionEntity.setFlgDel(1);
                positionMapper.updateById(positionEntity);
            }
        }

        return CIOUtil.buildRetMsg(true, "删除成功", null);
    }


}
